package com.lagou.edu.mvcframework.pojo;

import com.lagou.edu.mvcframework.annotations.Security;

import javax.sound.midi.MetaEventListener;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;


/**
 * 封装handler方法相关的信息
 */
public class Handler {

    private Object controller; // method.invoke(obj,)

    private Method method;

    private Pattern pattern; // spring中url是支持正则的

    private Map<String,Integer> paramIndexMapping; // 参数顺序,是为了进行参数绑定，key是参数名，value代表是第几个参数 <name,2>

    //Controller方法上的角色
    private String[] controllerUser;

    //方法上的角色，细粒度的权限控制
    private String[] methodUser;


    public Handler(Object controller, Method method, Pattern pattern) {
        this.controller = controller;
        this.method = method;
        this.pattern = pattern;
        this.paramIndexMapping = new HashMap<>();
        if(controller.getClass().isAnnotationPresent(Security.class)){
            Security security = controller.getClass().getAnnotation(Security.class);
            this.controllerUser = security.user();
        }
        if(method.isAnnotationPresent(Security.class)){
            Security security = method.getAnnotation(Security.class);
            this.methodUser = security.user();
        }
    }

    public Object getController() {
        return controller;
    }

    public void setController(Object controller) {
        this.controller = controller;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public Pattern getPattern() {
        return pattern;
    }

    public void setPattern(Pattern pattern) {
        this.pattern = pattern;
    }

    public Map<String, Integer> getParamIndexMapping() {
        return paramIndexMapping;
    }

    public void setParamIndexMapping(Map<String, Integer> paramIndexMapping) {
        this.paramIndexMapping = paramIndexMapping;
    }

    public String[] getControllerUser() {
        return controllerUser;
    }

    public void setControllerUser(String[] controllerUser) {
        this.controllerUser = controllerUser;
    }

    public String[] getMethodUser() {
        return methodUser;
    }

    public void setMethodUser(String[] methodUser) {
        this.methodUser = methodUser;
    }
}
